<?php
class PreparationREQPageAdmin
{
    //Variables qui contiendront la  pr�paration des requ�tes
    private $ReqSelectById;
    private $ReqSelectByName;
    private $ReqSelectAll;
    private $ReqSelectAllPage;
    private $ReqTrouverID;
    private $ReqTrouverDernierID;
    private $ReqInsert;
    private $ReqTruncate;
    private $ReqSelectSousPageByPageID;


    public function __construct($db)
    {
    	//variables renseign�es lors de la pr�paration des requ�tes
	
        $this->ReqSelectAllPage = $db->prepare("SELECT * FROM page ORDER BY PAG_ID");
		
        $this->ReqSelectById = $db->prepare("SELECT * FROM page WHERE PAG_ID = ?");
        
        $this->ReqSelectSousPageByPageID = $db->prepare("SELECT sp_titre, sp_url FROM sous_page WHERE SP_ID_COMPOSANT = ?");
       
 	    $this->ReqTrouverDernierID = $db->prepare("SELECT IMID FROM image WHERE IMID = LAST_INSERT_ID()");
        
 	    $this->ReqTrouverID = $db->prepare("SELECT PAG_ID FROM page WHERE PAG_TITRE = ? AND PAG_URL = ? AND PAG_LOGO = ?");
 	    
		$this->ReqInsert = $db->prepare("INSERT INTO page(PAG_TITRE, PAG_URL, PAG_LOGO) VALUES (:PAG_TITRE, :PAG_URL, :PAG_LOGO)");
		
       $this->ReqTruncate = $db->prepare("TRUNCATE Image");
	   
    }

//M�thodes publiques qui ex�cuteront les requ�tes d�j� pr�par�es dans le constructeur.

    public function SelectPage()
    {
    	$this->ReqSelectAllPage->execute();
    	return $this->ReqSelectAllPage->fetchAll();
    }
	public function insert($vtitre, $vurl, $vlogo)
    {
		$this->ReqInsert->execute(array(':PAG_TITRE' => $vtitre, ':PAG_URL' => $vurl, ':PAG_LOGO' => $vlogo));
    	
        return $this->ReqTrouverDernierID->fetch();
    }
    
    public function TrouverSousPage($IDPage)
    {
    	$this->ReqSelectSousPageByPageID->execute(array($IDPage));
    	return $this->ReqSelectSousPageByPageID->fetch();
    }
    
    public function TrouverID ($vtitre,$vurl,$vlogo)
    {
    	$this->ReqTrouverID->execute(array($vtitre, $vurl, $vlogo));
    	return $this->ReqTrouverID->fetch();
    }

    public function selectAll()
    {
    	$this->ReqSelectAll->execute();
        return $this->ReqSelectAll->fetchAll();
    }

	public function selectById($id)
    {
        $this->ReqSelectById->execute(array($id));
        return $this->ReqSelectById->fetch();
    }

    public function selectByName($name)
    {
        $this->ReqSelectByName->execute(array('%'.$name.'%'));
        return $this->ReqSelectByName->fetchAll();
    }
    
	public function truncate()
    {
        return $this->ReqTruncate->execute();
    }
	
}